﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using SGP;
using System.Xml;
using System.Xml.Linq;
using System.IO;


namespace FusionAnimationEditior
{
    public partial class Form1 : Form
    {
        DateTime current;
        DateTime last;
        float delta;
        bool islooping;
        bool isAnimating;
        int id;
        string filePath;

        bool isAnchor;
        bool isSrcRect;
        bool isActRect;
        bool isColRect;
        bool isDrawing;

        bool isOnPanel;
        bool isPlaying;


        Point ScrollOffset;
       
        Point startPoint;
        Point endPoint;

        Point startPoint1;
        Point endPoint1;

        Point startPoint2;
        Point endPoint2;

        Point startPoint3;

        int picWidth;
        int picHeight;

        public int mousePosX;
        public int mousePosY;
        public int rectPosW;
        public int rectPosH;

        public Frames frames = new Frames();
        public Animation animation = new Animation();

        List<Frames> framesList = new List<Frames>();
        List<Animation> animationList = new List<Animation>();

        int DrawX = 0;
        int DrawY = 0;

        Point AnimationAnchor;
        Point OffsetAnchor;
        Point OffsetAnimationAnchor;

        ManagedDirect3D d3dManager;
        ManagedTextureManager textManager;

        public Form1()
        {
            InitializeComponent();

            d3dManager = ManagedDirect3D.Instance;
            textManager = ManagedTextureManager.Instance;

            d3dManager.InitManagedDirect3D(panel2, false);
            d3dManager.InitManagedDirect3D(panel1, false);

            textManager.InitManagedTextureManager(d3dManager.Device, d3dManager.Sprite);

            frames.trigger = "stuff";

            id = -1;
            islooping = true;
            isOnPanel = false;
            isDrawing = false;
        }

        public void Update()
        {
            last = current;
            current = DateTime.Now;

            //  delta = (current - last).TotalSeconds;
        }

        public void Render()
        {

            d3dManager.ChangeDisplayParam(panel2, false);


            d3dManager.DeviceBegin();
            d3dManager.SpriteBegin();

            d3dManager.Clear(Color.White);



            if (id != -1)
            {
                textManager.Draw(id, 0, 0, 1.0f, 1.0f, Rectangle.FromLTRB(DrawX, DrawY, DrawX + 1000, DrawY + 1000), 0, 0, 0, Color.White);
            }

            d3dManager.SpriteEnd();

            if (isOnPanel == true)
            {
                if (isSrcRect == true && isDrawing == true )
                {

                    d3dManager.DrawLine(startPoint.X, startPoint.Y, endPoint.X, startPoint.Y, Color.Red);
                    d3dManager.DrawLine(startPoint.X, startPoint.Y, startPoint.X, endPoint.Y, Color.Red);
                    d3dManager.DrawLine(endPoint.X, startPoint.Y, endPoint.X, endPoint.Y, Color.Red);
                    d3dManager.DrawLine(startPoint.X, endPoint.Y, endPoint.X, endPoint.Y, Color.Red);

                }

                if (isColRect == true)
                {
                    d3dManager.DrawLine(startPoint1.X, startPoint1.Y, endPoint1.X, startPoint1.Y, Color.Yellow);
                    d3dManager.DrawLine(startPoint1.X, startPoint1.Y, startPoint1.X, endPoint1.Y, Color.Yellow);
                    d3dManager.DrawLine(endPoint1.X, startPoint1.Y, endPoint1.X, endPoint1.Y, Color.Yellow);
                    d3dManager.DrawLine(startPoint1.X, endPoint1.Y, endPoint1.X, endPoint1.Y, Color.Yellow);

                }

                if (isActRect == true)
                {
                    ManagedDirect3D.Instance.DrawLine(startPoint2.X, startPoint2.Y, endPoint2.X, startPoint2.Y, Color.Blue);
                    ManagedDirect3D.Instance.DrawLine(startPoint2.X, startPoint2.Y, startPoint2.X, endPoint2.Y, Color.Blue);
                    ManagedDirect3D.Instance.DrawLine(endPoint2.X, startPoint2.Y, endPoint2.X, endPoint2.Y, Color.Blue);
                    ManagedDirect3D.Instance.DrawLine(startPoint2.X, endPoint2.Y, endPoint2.X, endPoint2.Y, Color.Blue);

                }
                if (isAnchor == true && isDrawing == true)
                {
                    //ManagedDirect3D.Instance.DrawLine(startPoint3.X, startPoint3.Y, endPoint3.X, startPoint3.Y, Color.Orange);
                    //ManagedDirect3D.Instance.DrawLine(startPoint3.X, startPoint3.Y, startPoint3.X, endPoint3.Y, Color.Orange);
                    //ManagedDirect3D.Instance.DrawLine(endPoint3.X, startPoint3.Y, endPoint3.X, endPoint3.Y, Color.Orange);
                    //ManagedDirect3D.Instance.DrawLine(startPoint3.X, endPoint3.Y, endPoint3.X, endPoint3.Y, Color.Orange);

                    ManagedDirect3D.Instance.DrawLine(startPoint3.X - 2, startPoint3.Y - 2, startPoint3.X + 2, startPoint3.Y + 2, Color.Orange);
                    ManagedDirect3D.Instance.DrawLine(startPoint3.X + 2, startPoint3.Y + 2, startPoint3.X - 2, startPoint3.Y - 2, Color.Orange);
                }



            }

            ManagedDirect3D.Instance.DeviceEnd();

            ManagedDirect3D.Instance.Present();

            if (isPlaying == true)
            {

                if (ListofFrames.Items.Count > 0)
                {
                    d3dManager.ChangeDisplayParam(panel1, true);



                    AnimationAnchor.X = 100;
                    AnimationAnchor.Y = 100;

                    OffsetAnchor.X = framesList[ListofFrames.SelectedIndex].SourceRect.X - framesList[ListofFrames.SelectedIndex].anchorPoint.X;
                    OffsetAnchor.Y = framesList[ListofFrames.SelectedIndex].SourceRect.Y - framesList[ListofFrames.SelectedIndex].anchorPoint.Y;


                    // AnimationAnchor.X =   AnimationAnchor.X - OffsetAnchor.X;
                    //AnimationAnchor.Y =   AnimationAnchor.Y - OffsetAnchor.Y;





                    d3dManager.DeviceBegin();
                    d3dManager.SpriteBegin();


                    d3dManager.Clear(Color.White);


                    textManager.Draw(id, AnimationAnchor.X - OffsetAnchor.X, AnimationAnchor.Y - OffsetAnchor.Y, 1.0f, 1.0f, framesList[ListofFrames.SelectedIndex].SourceRect, 0, 0, 0, 0);


                    d3dManager.SpriteEnd();
                    d3dManager.DeviceEnd();


                    if (ListofFrames.SelectedIndex < 5 && isAnimating == true)
                    {
                        ListofFrames.SelectedIndex++;

                    }

                    d3dManager.Present();


                    if (ListofFrames.SelectedIndex == 5 && isAnimating == true)
                    {
                        ListofFrames.SelectedIndex = 0;

                    }




                }

            }



        }

        public bool Looping()
        {

            return islooping;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }



        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void LoadFile_Click(object sender, EventArgs e)
        {

            OpenFileDialog fdlg = new OpenFileDialog();

            if (DialogResult.OK == fdlg.ShowDialog())
            {

                filePath = fdlg.FileName;

                id = ManagedTextureManager.Instance.LoadTexture(filePath, 0);

             picWidth =    ManagedTextureManager.Instance.GetTextureWidth(id);
              picHeight =  ManagedTextureManager.Instance.GetTextureHeight(id);
            }


        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {


        }

        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
        }


        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {


            if (isAnchor == true)
            {




                //  mousePosX = MousePosition.X;
                //  mousePosY = MousePosition.Y;

                startPoint3.X = e.Location.X;
                startPoint3.Y = e.Location.Y;





            }
            else if (isSrcRect == true)
            {

                startPoint.X = e.X;
                startPoint.Y = e.Y;


                frames.SourceRect.X = e.X;
                frames.SourceRect.Y = e.Y;

                SourceRectLeft.Value = frames.SourceRect.X;
                SourceRectTop.Value = frames.SourceRect.Y;

                isOnPanel = true;
                isSrcRect = true;


                if (SourceRectLeft.Value < 0)
                {
                    SourceRectLeft.Value *= -1;

                }
                if (SourceRectTop.Value < 0)
                {

                    SourceRectTop.Value *= -1;


                }


            }
            else if (isActRect == true)
            {
                startPoint2.X = e.X;
                startPoint2.Y = e.Y;


                frames.ActiveRect.X = e.X;
                frames.ActiveRect.Y = e.Y;

                ActiveRectLeft.Value = frames.ActiveRect.X;
                ActiveRectTop.Value = frames.ActiveRect.Y;

                isOnPanel = true;
                isActRect = true;

                if (ActiveRectLeft.Value < 0)
                {
                    ActiveRectLeft.Value *= -1;

                }
                if (ActiveRectTop.Value < 0)
                {

                    ActiveRectTop.Value *= -1;


                }



            }
            else if (isColRect == true)
            {


                startPoint1.X = e.X;
                startPoint1.Y = e.Y;

                frames.CollisionRect.X = e.X;
                frames.CollisionRect.Y = e.Y;

                CollisionRectLeft.Value = frames.CollisionRect.X;
                CollisionRectTop.Value = frames.CollisionRect.Y;

                isOnPanel = true;

                if (CollisionRectLeft.Value < 0)
                {
                    CollisionRectLeft.Value *= -1;

                }
                if (CollisionRectTop.Value < 0)
                {

                    CollisionRectTop.Value *= -1;


                }



            }
        }

        private void panel2_MouseUp(object sender, MouseEventArgs e)
        {




            if (isAnchor == true)
            {
               // Point anchor = e.Location;

               // startPoint3.X = e.X;
              //  startPoint3.Y = e.Y;

                frames.anchorPoint.X = startPoint3.X;
                frames.anchorPoint.Y = startPoint3.Y;

                AnchorPointX.Value = frames.anchorPoint.X;
                AnchorPointY.Value = frames.anchorPoint.Y;

                // anchor.X = e.Location.X;
                // anchor.Y = e.Location.Y;

            }
            else if (isSrcRect == true)
            {
                endPoint.X = e.X;
                endPoint.Y = e.Y;

                frames.SourceRect.Width = e.X - frames.SourceRect.X;
                frames.SourceRect.Height = e.Y - frames.SourceRect.Y;


                SourceRectRight.Value = frames.SourceRect.Width;
                SourceRectBottom.Value = frames.SourceRect.Height;
                isDrawing = true;
                isSrcRect = true;

                if (SourceRectRight.Value < 0)
                {
                    SourceRectRight.Value *= -1;
                    SourceRectLeft.Value -= SourceRectRight.Value;
                }
                if (SourceRectBottom.Value < 0)
                {

                    SourceRectBottom.Value *= -1;
                    SourceRectTop.Value -= SourceRectBottom.Value;

                }


            }
            else if (isActRect == true)
            {

                endPoint2.X = e.X;
                endPoint2.Y = e.Y;


                frames.ActiveRect.Width = e.X - frames.ActiveRect.X;
                frames.ActiveRect.Height = e.Y - frames.ActiveRect.Y;

                ActiveRectRight.Value = frames.ActiveRect.Width;
                ActiveRectBottom.Value = frames.ActiveRect.Height;

                isDrawing = true;



                if (ActiveRectRight.Value < 0)
                {
                    ActiveRectRight.Value *= -1;
                    ActiveRectLeft.Value -= ActiveRectRight.Value;
                }
                if (ActiveRectBottom.Value < 0)
                {

                    ActiveRectBottom.Value *= -1;
                    ActiveRectTop.Value -= ActiveRectBottom.Value;

                }


            }
            else if (isColRect == true)
            {

                endPoint1.X = e.X;
                endPoint1.Y = e.Y;

                frames.CollisionRect.Width = e.X - frames.CollisionRect.X;
                frames.CollisionRect.Height = e.Y - frames.CollisionRect.Y;

                CollisionRectRight.Value = frames.CollisionRect.Width;
                CollisionRectBottom.Value = frames.CollisionRect.Height;

                isDrawing = true;
                isColRect = true;


                if (CollisionRectRight.Value < 0)
                {
                    CollisionRectRight.Value *= -1;
                    CollisionRectLeft.Value -= CollisionRectRight.Value;
                }
                if (CollisionRectBottom.Value < 0)
                {

                    CollisionRectBottom.Value *= -1;
                    CollisionRectTop.Value -= CollisionRectBottom.Value;

                }



            }




        }








        private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            mousePosX = e.X;
            mousePosY = e.Y;


            if (isSrcRect == true && e.Button == MouseButtons.Left)
            {


                endPoint.X = e.X;
                endPoint.Y = e.Y;


                isDrawing = true;
            }
            else if (isActRect == true && e.Button == MouseButtons.Left)
            {


                endPoint2.X = e.X;
                endPoint2.Y = e.Y;

                isDrawing = true;
            }
            else if (isColRect == true && e.Button == MouseButtons.Left)
            {


                endPoint1.X = e.X;
                endPoint1.Y = e.Y;

                isDrawing = true;
            }
            else if (isAnchor == true && e.Button == MouseButtons.Left)
            {


                startPoint3.X = e.X;
                startPoint3.Y = e.Y;

                isDrawing = true;
            }




        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            islooping = false;
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            //int hPos1 = e.OldValue;
            //int hPos2 = e.NewValue;

            ////int offsetX = hPos2 - hPos1;

            //if (hPos2 > hPos1)
            //{
            //    DrawX += hPos1;

            //    hPos2 = hPos1 - DrawX;
            //}
            //else if (hPos2 < hPos1)
            //{
            //    DrawX -= hPos1;

            //    hPos2 = hPos1 + DrawX;
            //}
            ////  offsetX -= hPos;

          //  ScrollOffset.X = (float)hScrollBar1.Value * 0.01f;


        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            int hPos1 = e.OldValue;
            int hPos2 = e.NewValue;

            //int offsetX = hPos2 - hPos1;

            if (hPos2 > hPos1)
            {
                DrawY += hPos1;

                hPos2 = hPos1;
            }
            else if (hPos2 < hPos1)
            {
                DrawY -= hPos1;

                hPos2 = hPos1;
            }

        }

        private void SrcRect_Click(object sender, EventArgs e)
        {

        }

        private void ActiveRect_Click(object sender, EventArgs e)
        {

        }

        private void CollisionRect_Click(object sender, EventArgs e)
        {

        }

        private void AnchorPoint_MouseClick(object sender, MouseEventArgs e)
        {
            isAnchor = true;
            isSrcRect = false;
            isActRect = false;
            isColRect = false;
        }

        private void CollisionRect_MouseClick(object sender, MouseEventArgs e)
        {
            isAnchor = false;
            isSrcRect = false;
            isActRect = false;
            isColRect = true;
        }

        private void ActiveRect_MouseClick(object sender, MouseEventArgs e)
        {
            isAnchor = false;
            isSrcRect = false;
            isActRect = true;
            isColRect = false;
        }

        private void SrcRect_MouseClick(object sender, MouseEventArgs e)
        {
            isAnchor = false;
            isSrcRect = true;
            isActRect = false;
            isColRect = false;
        }

        private void SourceRectLeft_ValueChanged(object sender, EventArgs e)
        {
            startPoint.X = (int)SourceRectLeft.Value;
        }

        private void SourceRectTop_ValueChanged(object sender, EventArgs e)
        {
            startPoint.Y = (int)SourceRectTop.Value;
        }

        private void SourceRectRight_ValueChanged(object sender, EventArgs e)
        {
            int rightValue;
            rightValue = endPoint.X;
            rightValue = (int)SourceRectRight.Value;

            
        }

        private void SourceRectBottom_ValueChanged(object sender, EventArgs e)
        {
            int botValue;
            botValue = startPoint.Y + endPoint.Y;
            botValue = (int)SourceRectBottom.Value;

            botValue = endPoint.Y;

        }

        private void ActiveRectLeft_ValueChanged(object sender, EventArgs e)
        {
            startPoint2.X = (int)ActiveRectLeft.Value;
        }

        private void ActiveRectTop_ValueChanged(object sender, EventArgs e)
        {
            startPoint2.Y = (int)ActiveRectTop.Value;
        }

        private void ActiveRectRight_ValueChanged(object sender, EventArgs e)
        {
            int rightValue1;
            rightValue1 = endPoint2.X - startPoint2.X;
            rightValue1 = (int)ActiveRectRight.Value;
        }

        private void ActiveRectBottom_ValueChanged(object sender, EventArgs e)
        {
            int botValue1;
            botValue1 = endPoint2.Y + startPoint2.Y;
            botValue1 = (int)ActiveRectBottom.Value;

            botValue1 = endPoint2.Y;
        }

        private void CollisionRectLeft_ValueChanged(object sender, EventArgs e)
        {
            startPoint1.X = (int)CollisionRectLeft.Value;
        }

        private void CollisionRectTop_ValueChanged(object sender, EventArgs e)
        {
            startPoint1.Y = (int)CollisionRectTop.Value;
        }

        private void CollisionRectRight_ValueChanged(object sender, EventArgs e)
        {
            int rightValue2;
              rightValue2 = endPoint1.X - startPoint1.X;
              rightValue2  = (int)CollisionRectRight.Value;
        }

        private void CollisionRectBottom_ValueChanged(object sender, EventArgs e)
        {
            int botValue2;
            botValue2 = endPoint1.Y + startPoint1.Y;
            botValue2= (int)CollisionRectBottom.Value;

            botValue2 = endPoint2.Y;
        }

        private void AnchorPointY_ValueChanged(object sender, EventArgs e)
        {
            startPoint3.Y = (int)AnchorPointY.Value;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            startPoint3.X = (int)AnchorPointX.Value;
        }

        private void AddtoAnimation_Click(object sender, EventArgs e)
        {
            List<Frames> tempFrames = new List<Frames>(framesList);

            //tempFrames = framesList;
            Animation addAnimation = new Animation();

            addAnimation.Frames = tempFrames;


            animationList.Add(addAnimation);

            ListofAnimations.Items.Add("Animation" + (animationList.Count - 1));
            ListofAnimations.SelectedIndex = 0;

            ListofFrames.Items.Clear();
            framesList.Clear();
        }

        private void AddFrame_Click(object sender, EventArgs e)
        {
            Frames addFrame = new Frames();

            addFrame.SourceRect = frames.SourceRect;
            addFrame.anchorPoint = frames.anchorPoint;
            addFrame.ActiveRect = frames.ActiveRect;
            addFrame.CollisionRect = frames.CollisionRect;
           // addFrame.trigger = frames.trigger;

            framesList.Add(addFrame);
            ListofFrames.Items.Add("Frame" + (framesList.Count - 1));
            ListofFrames.SelectedIndex = 0;



            isAnchor = false;
           // isSrcRect = false;
            isActRect = false;
            isColRect = false;
        }

        private void PlayButton_Click(object sender, EventArgs e)
        {
            isPlaying = true;

            isAnimating = true;
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            isAnimating = false;
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void SaveImage_Click(object sender, EventArgs e)
        {

            ListofAnimations.SelectedIndex = 0;

            SaveFileDialog sdlg = new SaveFileDialog();

            if (DialogResult.OK == sdlg.ShowDialog())
            {

                filePath = sdlg.FileName;

                XElement root = new XElement("AllAnimations");

                for (int i = 0; i < 7; i++)
                {
                    XElement animations = new XElement("Animations");
                    // root.Add(frames);
                    for (int x = 0; x < 6; x++)
                    {
                        XElement frames = new XElement("Frames");


                        XElement sourceRect = new XElement("SourceRect");

                        Frames frameObj = new Frames();

                        XAttribute sourceLeft = new XAttribute("RenderLeft", animationList[i].Frames[x].SourceRect.Left);
                        XAttribute sourceTop = new XAttribute("RenderTop", animationList[i].Frames[x].SourceRect.Top);
                        XAttribute sourceRight = new XAttribute("RenderRight", animationList[i].Frames[x].SourceRect.Right);
                        XAttribute sourceBottom = new XAttribute("RenderBottom", animationList[i].Frames[x].SourceRect.Bottom);

                        sourceRect.Add(sourceLeft);
                        sourceRect.Add(sourceTop);
                        sourceRect.Add(sourceRight);
                        sourceRect.Add(sourceBottom);




                        XElement collisionRect = new XElement("CollisionRect");

                        XAttribute collisionLeft = new XAttribute("CollisionLeft", animationList[i].Frames[x].CollisionRect.Left);
                        XAttribute collisionTop = new XAttribute("CollisionTop", animationList[i].Frames[x].CollisionRect.Top);
                        XAttribute collisionRight = new XAttribute("CollisionRight", animationList[i].Frames[x].CollisionRect.Right);
                        XAttribute collisionBottom = new XAttribute("CollisionBottom", animationList[i].Frames[x].CollisionRect.Bottom);

                        collisionRect.Add(collisionLeft);
                        collisionRect.Add(collisionTop);
                        collisionRect.Add(collisionRight);
                        collisionRect.Add(collisionBottom);

                        XElement activeRect = new XElement("ActiveRect");

                        XAttribute activeLeft = new XAttribute("ActiveLeft", animationList[i].Frames[x].ActiveRect.Left);
                        XAttribute activeTop = new XAttribute("ActiveTop", animationList[i].Frames[x].ActiveRect.Top);
                        XAttribute activeRight = new XAttribute("ActiveRight", animationList[i].Frames[x].ActiveRect.Right);
                        XAttribute activeBottom = new XAttribute("ActiveBottom", animationList[i].Frames[x].ActiveRect.Bottom);

                        activeRect.Add(activeLeft);
                        activeRect.Add(activeTop);
                        activeRect.Add(activeRight);
                        activeRect.Add(activeBottom);



                        XElement anchorPoint = new XElement("AnchorPoint");

                        XAttribute AnchorX = new XAttribute("X", animationList[i].Frames[x].anchorPoint.X);
                        XAttribute AnchorY = new XAttribute("Y", animationList[i].Frames[x].anchorPoint.Y);

                        anchorPoint.Add(AnchorX);
                        anchorPoint.Add(AnchorY);

                        //  XAttribute trigger = new XAttribute("Trigger", addAnimation.Frames[x].trigger);

                        //trigger.Value = "Stuff";

                        //  trigger.Add(trigger.Value);


                        frames.Add(sourceRect);
                        frames.Add(collisionRect);
                        frames.Add(activeRect);
                        frames.Add(anchorPoint);
                        //frames.Add(trigger);

                        animations.Add(frames);
                    }
                    root.Add(animations);
                }
                root.Save(filePath);
            }

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void EditFrame_Click(object sender, EventArgs e)
        {           

          framesList[ListofFrames.SelectedIndex].anchorPoint.X = (int)AnchorPointX.Value;
          framesList[ListofFrames.SelectedIndex].anchorPoint.Y = (int)AnchorPointY.Value;

          AnchorPointX.Value = framesList[ListofFrames.SelectedIndex].anchorPoint.X;
          AnchorPointY.Value = framesList[ListofFrames.SelectedIndex].anchorPoint.Y;
        }

        private void ListofFrames_SelectedIndexChanged(object sender, EventArgs e)
        {
            AnchorPointX.Value = framesList[ListofFrames.SelectedIndex].anchorPoint.X;
            AnchorPointY.Value = framesList[ListofFrames.SelectedIndex].anchorPoint.Y;
        }



    }
}
